<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="所属分类" prop="messageClassify">
        <el-select v-model="queryParams.messageClassify" placeholder="请选择所属分类" clearable size="small">
          <el-option
            v-for="dict in messageClassifyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="headline">
        <el-input
          v-model="queryParams.headline"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon=""
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shopping:bbsUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shopping:bbsUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shopping:bbsUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['shopping:bbsUser:export']"
          v-if="false"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="bbsUserList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip />-->
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="所属分类" align="center" prop="messageClassify">
        <template slot-scope="scope">
          <dict-tag :options="messageClassifyOptions" :value="scope.row.messageClassify"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="headline" />
      <el-table-column label="正文" align="center" prop="mainBody" :show-overflow-tooltip="true"/>
  <!--    <el-table-column label="视屏列表" align="center" prop="videoList" >
        <template slot-scope="scope">
          <div v-if="scope.row.videoList">
            <el-button
              type="success"
              plain
              @click="playVideo(scope.row.videoList)"
              ref="btn"
              size="mini"
            >播 放</el-button>
          </div>
          <div v-else>暂无视频</div>
        </template>
      </el-table-column>-->
<!--      <el-table-column label="所属端" align="center" prop="belongs" />-->
    <!--  <el-table-column label="图片列表" align="center" prop="imageList" >
        <template slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.imgFirst" >
        </template>
      </el-table-column>-->

<!--      <el-table-column label="发布人id" align="center" prop="publishId" />-->
      <el-table-column label="发布人名称" align="center" prop="publishName" />
      <el-table-column label="发布人头像" align="center" prop="publishHead" >
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.publishHead">
        </template>
      </el-table-column>
     <!-- <el-table-column label="是否有回复信息" align="center" prop="replyMessage">
        <template slot-scope="scope">
          <dict-tag :options="replyMessageOptions" :value="scope.row.replyMessage"/>
        </template>
      </el-table-column>
      <el-table-column label="回复信息数量" align="center" prop="replyMessageNumber" />-->
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="上级id" align="center" prop="parentId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['shopping:bbsUser:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleBbsUser(scope.row)"
            v-hasPermi="['shopping:bbsUser:edit']"
          >评论详情</el-button>
          <el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"
            v-hasPermi="['shopping:bbsUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 视频 -->
    <el-dialog
      title="视频预览"
      :visible.sync="dialogPlay"
      width="30%"
      @close="closeDialog"
    >
      <video
        :src="videoList"
        controls
        autoplay
        class="video"
        ref="dialogVideo"
        width="100%"
      ></video>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户端留言对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="700px" append-to-body>
      <div class="el-dialog-div">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="所属分类" prop="messageClassify">
          <el-select v-model="form.messageClassify" placeholder="请选择所属分类" disabled readonly>
            <el-option
              v-for="dict in messageClassifyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="headline">
          <el-input v-model="form.headline" placeholder="请输入标题" readonly/>
        </el-form-item>
        <el-form-item label="正文" prop="mainBody">
          <el-input v-model="form.mainBody" type="textarea" placeholder="请输入内容" readonly/>
        </el-form-item>
        <el-form-item label="视频" prop="videoList" v-if="form.videoList!=undefined">
          <VideoScan :list="videos" ref="videoScan" v-if="open"></VideoScan>
        </el-form-item>
<!--        <el-form-item label="所属端" prop="belongs">-->
<!--          <el-input v-model="form.belongs" placeholder="请输入所属端" />-->
<!--        </el-form-item>-->
        <el-form-item label="图片列表">
          <image-upload  :disabled="true":limit="0" v-model="form.imageList" />
        </el-form-item>

<!--        <el-form-item label="发布人id" prop="publishId">-->
<!--          <el-input v-model="form.publishId" placeholder="请输入发布人id" />-->
<!--        </el-form-item>-->
        <el-form-item label="发布人名称" prop="publishName">
          <el-input v-model="form.publishName" placeholder="请输入发布人名称" readonly/>
        </el-form-item>
        <el-form-item label="发布人头像" >
          <image-upload  :limit="1" :multiple="false" v-model="form.publishHead" />
        </el-form-item>
       <!-- <el-form-item label="是否有回复信息"  prop="replyMessage">
          <el-select v-model="form.replyMessage" placeholder="请选择是否有回复信息" disabled readonly>
            <el-option
              v-for="dict in replyMessageOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回复信息数量" prop="replyMessageNumber">
          <el-input v-model="form.replyMessageNumber" placeholder="请输入回复信息数量" readonly/>
        </el-form-item>-->
       <!-- <el-form-item label="上级id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入上级id" />
        </el-form-item>-->
      </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBbsUser, getBbsUser, logicDelBbsUser, addBbsUser, updateBbsUser, exportBbsUser } from "@/api/shopping/bbsUser";

export default {
  name: "BbsUser",
  data() {
    return {
      // 视频播放
      dialogPlay: false,
      videoList:"",
      // 图片轮播
      imageList:[],
      //视频列表
      videos:[],
      // videosOpen:true,

      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 用户端留言表格数据
      bbsUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 所属分类字典
      messageClassifyOptions: [],
      // 发布端字典
      publishOptions: [],
      // 是否有回复信息字典
      replyMessageOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        messageClassify: null,
        headline: null,
        mainBody: null,
        videoList: null,
        belongs: null,
        imageList: null,
        publish: null,
        publishId: null,
        publishName: null,
        publishHead: null,
        replyMessage: null,
        replyMessageNumber: null,
        parentId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.calHeight()
      this.getDicts("bbs_user_classify").then(response => {
      this.messageClassifyOptions = response.data;
    });
    this.getDicts("publish").then(response => {
      this.publishOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.replyMessageOptions = response.data;
    });
  },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    // 视频
    playVideo(url) {
      this.dialogPlay = true;
      this.videoList = url;
    },
    closeDialog() {
      this.videoList = ""; //清空数据 关闭视频播放
    },
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询用户端留言列表 */
    getList() {
      this.loading = true;
      listBbsUser(this.queryParams).then(response => {
        this.bbsUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
        // for(var j = 0; j < response.rows.length; j++){
        //   let item = response.rows[j]
        //   if(!item.videoList){
        //     let imgList = JSON.parse(item.imageList)
        //     let arr = []
        //     for (var i = 0; i<imgList.length;i++){
        //       arr.push(imgList.src)
        //     }
        //     item.imgFirst =arr
        //   }else{
        //     item.imgFirst = null
        //   }
        // }
    /** 跳转到论坛评论 */
    handleBbsUser(row){
      this.$router.push({ path: '/bbs/bbsComments', query: { id: row.id }})
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        messageClassify: null,
        headline: null,
        mainBody: null,
        videoList: null,
        belongs: null,
        imageList: null,
        publish: null,
        publishId: null,
        publishName: null,
        publishHead: null,
        replyMessage: null,
        replyMessageNumber: null,
        parentId: null,
        delState: null,
        delUser: null,
        delType: null,
        delTime: null,
        createBy: null,
        createType: null,
        createTime: null,
        updateBy: null,
        updateType: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户端留言";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBbsUser(id).then(response => {
        console.log(response)
        if(response.data.imageList){
          let arr = []
          let imgList = JSON.parse(response.data.imageList)
          for(let img of imgList){
            arr.push(img.src)
          }
          response.data.imageList = arr
        }
        this.form = response.data;
        this.open = true;
        // this.videosOpen = true
        let videos = response.data.videoList;
        if(videos){
          this.videos= videos.split(",");
          // this.videosOpen = false;
        }else{
          this.videos= [];
        }
        this.title = "修改用户端留言";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBbsUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBbsUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicDelBbsUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户端留言数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportBbsUser(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>

</style>
<style lang="scss" scoped>
  .el-dialog-div{
    height: 60vh;
    overflow: auto;
  }
  .el-tooltip__popper{
    max-width:20%;
  }
  .el-tooltip__popper,.el-tooltip__popper.is-dark{
    background:rgb(48, 65, 86) !important;
    color: #fff !important;
    line-height: 24px;
  }
</style>

